<?php
	
	class ShoppingCart extends WebService_base{
		var $cart;
		var $customer_id;
		var $sid;
		var $total; 
				
		/**
		 * Constructor
		 *
		 */
		function __construct(){
			parent::__construct();
			if($this->ws_get_config()){
				$GLOBALS['db_link'] = $this->db_connect();
			}					
			if(isset($_COOKIE['ws_freeway'])){
				session_name('ws_freeway');
				$this->sid = session_id($_COOKIE['ws_freeway']);
				session_start();
			}
			if(isset($_SESSION['customer_id'])){
				$this->customer_id = $_SESSION[customer_id];
				$_COOKIE['customer_id'] = $_SESSION['customer_id']; 
			}
			if(isset($_COOKIE['customer_id'])){
				$this->customer_id = $_COOKIE['customer_id'];
				$_SESSION[customer_id] = $this->customer_id;
			}
			else{
				$this->customer_id = $this->sid;
			}
		}
		
		/**
		 * Return all Session vars for debugging
		 *
		 * @return mixed
		 */
		function SessionVars(){
			return $_SESSION;	
		}
	
		/**
		 * Add Event to Guest Cart
		 *
		 * @param string $products_id
		 * @param string $customers_basket_quantity
		 * @param string $products_type
		 * @param string $old_orders_id
		 * @param string $answer_type
		 * @param string $answer_value
		 * @param string $session_id
		 * @return mixed
		 */
		function GuestBasketAdd($products_id, $customers_basket_quantity = 1, $products_type = 'E', $old_orders_id = 0, $answer_type = '', $answer_value = '', $session_id){
			$customer_id = $this->customer_id;
			if($customer_id != $this->sid){
				return $this->CustomerBasketAdd($products_id, $customers_basket_quantity, $products_type, $old_orders_id, $answer_type, $answer_value, $session_id);
			}
			$SESSIONS_TABLE = TABLE_CUSTOMERS_TEMP_BASKET_SESSIONS;
			$BASKET_TABLE = TABLE_CUSTOMERS_TEMP_BASKET;
			$link = $this->db_connect();
			if($link){
				$sessions_inserts = array();
				$basket_inserts = array();
				$select_session_details = 'Select * from '. TABLE_EVENTS_SESSIONS . ' WHERE sessions_id=' .$session_id;
				$ssd_results = $this->db_query($select_session_details, $link);
				$errors[] = mysql_error($link);
				if($this->db_num_rows($ssd_results)>0){
						while($row = mysql_fetch_assoc($ssd_results)){
							$sql = "insert into ". $SESSIONS_TABLE ." (customers_id,products_id,session_id,session_type,session_date) "; 
							$sql .= " values ('$customer_id','$products_id','$row[session_id]','$row[sessions_type]','$row[start_date]')";
							$sessions_inserts[] = $sql;						
							$errors[] = array(mysql_error($link),  $sql2);							
						}
				} 
				$sql = "insert into " . $BASKET_TABLE; 
				$sql .= "(customers_id, products_id, customers_basket_quantity, customers_basket_date_added,products_type,old_orders_id,answer_type,answer_value)";
				$sql .= "values ('$customer_id', '$products_id', '$customers_basket_quantity', now(),'$products_type','$old_orders_id','$answer_type','$answer_value')";	
				$basket_inserts[] = $sql;
				$errors[] = array(mysql_error($link),  $sql);
			}
			
			foreach($basket_inserts as $temp_basket) 
				$this->db_query($temp_basket, $link);
			unset($basket_inserts); 

			foreach($sessions_inserts as $temp_basket_sessions)
				 $this->db_query($temp_basket_sessions, $link);
			unset($sessions_inserts);
			return $errors;
		}
		
		/**
		 * Add Event to Customer's Cart
		 *
		 * @param string $products_id
		 * @param string $customers_basket_quantity
		 * @param string $products_type
		 * @param string $old_orders_id
		 * @param string $answer_type
		 * @param string $answer_value
		 * @param string $session_id
		 * @return mixed
		 */
		function CustomerBasketAdd($products_id, $customers_basket_quantity = 1, $products_type = 'E', $old_orders_id = 0, $answer_type = '', $answer_value = '', $session_id){
			$customer_id = $this->customer_id;
			if($customer_id == $this->sid){
				return $this->GuestBasketAdd($products_id, $customers_basket_quantity, $products_type, $old_orders_id, $answer_type, $answer_value, $session_id);
			}
			$SESSIONS_TABLE = TABLE_CUSTOMERS_BASKET_SESSIONS;
			$BASKET_TABLE = TABLE_CUSTOMERS_BASKET;
			$link = $this->db_connect();
			if($link){
				$sessions_inserts = array();
				$basket_inserts = array();
				$select_session_details = 'SELECT a.*, b.events_max_reserve FROM '. TABLE_EVENTS_SESSIONS . ' a, '.TABLE_EVENTS.' b WHERE a.sessions_id=' .$session_id.' AND a.events_id = b.events_id ';
				$select_session_bookings = 'SELECT SUM(products_quantity) as booked FROM orders_products WHERE products_id = '.$session_id;
				$ssd_results = $this->db_query($select_session_details, $link);
				$errors[] = mysql_error($link);
				$ssb_results = $this->db_query($select_session_bookings, $link);
				$errors[] = mysql_error($link);
				if($this->db_num_rows($ssd_results)>0 ){
					$value = mysql_fetch_assoc($ssb_results);
					$value2 = mysql_fetch_assoc($ssd_results);
					if(((int)$value[booked] + $customers_basket_quantity) < $value2[events_max_reserve]){
						mysql_data_seek($ssd_results, 0);
						while($row = mysql_fetch_assoc($ssd_results)){
							$sql = "insert into ". $SESSIONS_TABLE ." (customers_id,products_id,session_id,session_type,session_group,session_date) "; 
							$sql .= " values ('$customer_id','$session_id','$row[session_id]','$row[sessions_type]','$session_group','$row[start_date]')";
							$sessions_inserts[] = $sql;						

						}
						$sql = "insert into " . $BASKET_TABLE; 
						$sql .= " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added,products_type,old_orders_id,answer_type,answer_value) ";
						$sql .= "values ('$customer_id', '$products_id', '$customers_basket_quantity', now(),'$products_type','$old_orders_id','$answer_type','$answer_value')";	
						$basket_inserts[] = $sql;
						$errors[] = array(mysql_error($link),  $sql . ' m: ' . $value2[events_max_reserve] . ' b: ' . $value[booked], $value);
													
						foreach($basket_inserts as $temp_basket) 
							$this->db_query($temp_basket, $link);
						unset($basket_inserts); 

						foreach($sessions_inserts as $temp_basket_sessions)
							 $this->db_query($temp_basket_sessions, $link);
						unset($sessions_inserts);
					}
					else{
						if($value2[events_max_reserve] < $value[booked] + 0)
							$errors[booked] = 'The selected class does not have enough reamaining seats for your order.';
						else
							$errors[booked] = 'The selected class is fully booked.';
					}
				} 
			}
			return $errors;
		}

		/**
		 * Adds a customer as an attendee to an event
		 *
		 * @param int $sessions_id
		 * @param int $events_id
		 * @param int $customers_id
		 * @param string $customers_firstname
		 * @param string $customers_lastname
		 * @param string $customers_telephone
		 * @param string $customers_emailaddress
		 * @param string $customers_query
		 * @param string $customers_attend
		 * @param string $customers_paid
		 * @param string $customers_paid_type
		 * @param string $final_price
		 * @param string $paid
		 * @return string
		 */
		function AttendeeAdd($sessions_id, $events_id, $customers_id, $customers_firstname, $customers_lastname, $customers_telephone, $customers_emailaddress, $customers_query, $customers_attend, $customers_paid, $customers_paid_type, $final_price, $paid){

			$sqlAddAttendees = "insert into events_attendees (sessions_id, events_id , customers_id, customers_firstname, customers_lastname, customers_telephone, customers_emailaddress, customers_query, customers_attend, customers_paid, customers_paid_type, final_price, paid) ";			
			$sqlAddAttendees .="values ($sessions_id, $events_id, $customers_id, '$customers_firstname', '$customers_lastname', '$customers_telephone', '$customers_emailaddress', '$customers_query', '$customers_attend', '$customers_paid', '$customers_paid_type', '$final_price', '$paid')";
			$link = $this->db_connect();
			if($link){
				$sAA_results = $this->db_query($sqlAddAttendees, $link);
				if(mysql_error($link) == "")
					return "worked: " . $sqlAddAttendees;
				else 
					return $sqlAddAttendees;
			}
			else 
				return $sqlAddAttendees;
		}
		
		/**
		 * Empty cart for current Guest 
		 *
		 * @return mixed
		 */
		function ClearGuestCart(){
			$sid = $this->sid;
			$SESSIONS_TABLE = TABLE_CUSTOMERS_TEMP_BASKET_SESSIONS;
			$BASKET_TABLE = TABLE_CUSTOMERS_TEMP_BASKET;
			$errors = array();
			$link = $this->db_connect();
			if($link){
				$sql = "DELETE FROM " . $SESSIONS_TABLE . " WHERE customers_id = '". $sid ."'";
				$this->db_query($sql, $link);
				$errors[] = array(mysql_error($link),  $sql);
				$sql = "DELETE FROM " . $BASKET_TABLE . " WHERE customers_id =  '". $sid ."'";
				$this->db_query($sql, $link);
				$errors[] = array(mysql_error($link),  $sql);
			}
			return $errors;	
		}
		
		/**
		 * Empty Cart for current Customer
		 *
		 * @return mixed
		 */
		function ClearCustomerCart(){
			$customer_id = $this->customer_id;
			$SESSIONS_TABLE = TABLE_CUSTOMERS_BASKET_SESSIONS;
			$BASKET_TABLE = TABLE_CUSTOMERS_BASKET;
			$errors = array();
			$link = $this->db_connect();
			if($link){
				$sql = "DELETE FROM " . $SESSIONS_TABLE . " WHERE customers_id = '". $customer_id ."'";
				$this->db_query($sql, $link);
				$errors[] = array(mysql_error($link),  $sql);
				$sql = "DELETE FROM " . $BASKET_TABLE . " WHERE customers_id = '". $customer_id ."'";
				$this->db_query($sql, $link);
				$errors[] = array(mysql_error($link),  $sql);
			}
			return $errors;	
		}

		/**
		 * Remove 1 item from Cart by products_id
		 *
		 * @param int $products_id
		 * @return mixed
		 */
		function RemoveItem($products_id = 0){
			$customer_id = $this->customer_id;
			$SESSIONS_TABLE = TABLE_CUSTOMERS_BASKET_SESSIONS;
			$BASKET_TABLE = TABLE_CUSTOMERS_BASKET;
			$errors = array();
			$link = $this->db_connect();
			if($link){
				$sql = "DELETE FROM ". $BASKET_TABLE." WHERE products_id ='". $products_id . "' AND customers_id = '".$customer_id."' LIMIT 1";
				$this->db_query($sql, $link);
				$errors[] = array(mysql_error($link),  $sql);

				$sql = "DELETE FROM ". $SESSIONS_TABLE." WHERE products_id ='". $products_id . "' AND customers_id = '".$customer_id."' LIMIT 1";
				$this->db_query($sql, $link);
				$errors[] = array(mysql_error($link),  $sql);
			}
			return $errors;
		}

		/**
		 * Get Guest cart contents or Customer Cart Contents if user is logged in
		 *
		 * @return mixed
		 */
		function getGuestCart(){
			$sid = $this->sid;
			$customer_id = $this->customer_id;
			if($this->customer_id != $this->sid){
				return $this->getCustomerCart();
			}
			$SESSIONS_TABLE = TABLE_CUSTOMERS_TEMP_BASKET_SESSIONS;
			$BASKET_TABLE = TABLE_CUSTOMERS_TEMP_BASKET;
			$errors = array();
			$link = $this->db_connect();
			if($link){
				$sql[] = "SELECT * FROM " . $SESSIONS_TABLE . " WHERE customers_id = '". $sid ."'";
				$result = $this->db_query($sql[0], $link);
				if($this->db_num_rows($result) > 0)
					while($row = $this->db_fetch_array($result))
						$sessions[] = $row;
				$errors[] = array(mysql_error($link),  $sql);
				$sql[] = "SELECT * FROM " . $BASKET_TABLE . " WHERE customers_id =  '". $sid ."'";
				$result = $this->db_query($sql[1], $link);
				if($this->db_num_rows($result) > 0)
					while($row = $this->db_fetch_array($result))
						$basket[] = $row;

			}
			return array('sessions'=>$sessions, 'basket'=>$basket);
			//return $sql;	
		}
		
		/**
		 * Get Customer Shopping Cart Contents
		 *
		 * @return mixed
		 */
		function getCustomerCart(){
			$sid = $this->sid;
			$customer_id = $this->customer_id;
			$getEvents = "SELECT 	";
			$getEvents .= "a.events_categories_name, a.events_categories_heading_title, a.events_categories_description, "; 
			$getEvents .= "b.events_categories_id, ";
			$getEvents .= "c.events_name, c.events_id, c.events_description, c.events_teaser_text, ";
			$getEvents .= "e.events_status, e.events_fees, e.events_start_time, e.events_end_time, ";
			$getEvents .= "f.location_name, f.location_seats, f.location_details, ";
			$getEvents .= "g.sessions_id, count(*) as qnty ";
			$getEvents .= "FROM ";
			$getEvents .= "events_categories_description a, ";
			$getEvents .= "events_categories b, ";
			$getEvents .= "events_description c, ";
			$getEvents .= "events_to_events_categories d, ";
			$getEvents .= "events e, ";
			$getEvents .= "events_locations f, ";
			$getEvents .= "events_sessions g ";
			$getEvents .= "WHERE ";
			$getEvents .= "e.events_id = [[PROID]] ";	
			$getEvents .= "AND d.events_id = e.events_id ";
			$getEvents .= "AND e.events_id = g.events_id ";
			$getEvents .= "AND d.events_categories_id = b.events_categories_id ";
			$getEvents .= "AND a.events_categories_id = b.events_categories_id ";
			$getEvents .= "AND	c.events_id = d.events_id ";
			$getEvents .= "AND 	b.events_categories_id = d.events_categories_id ";
			$getEvents .= "AND	f.location_id = e.events_location ";
			$getEvents .= "GROUP BY 	c.events_id";
			
			
			if($this->customer_id == $this->sid){
				$this->getGuestCart();
			}
			$SESSIONS_TABLE = TABLE_CUSTOMERS_BASKET_SESSIONS;
			$BASKET_TABLE = TABLE_CUSTOMERS_BASKET;
			$errors = array();
			$link = $this->db_connect();
			if($link){
				$sql[] = "SELECT * FROM " . $SESSIONS_TABLE . " WHERE customers_id = '". $customer_id ."'";
				$result = $this->db_query($sql[0], $link);
				$errors[] = array(mysql_error($link),  $sql[0]);
				if($this->db_num_rows($result) > 0)				
					while($row = $this->db_fetch_array($result))
						$sessions[] = $row;

				$errors[] = array(mysql_error($link),  $sql);
				$sql[] = "SELECT *, sum(customers_basket_quantity) as qnty FROM " . $BASKET_TABLE . " WHERE customers_id =  '". $customer_id ."' GROUP BY products_id";
				$result = $this->db_query($sql[1], $link);
				$errors[] = array(mysql_error($link),  $sql[1]);
				if($this->db_num_rows($result) > 0)				
					while($row = $this->db_fetch_array($result)){
						$basket[] = $row;
						$query = str_replace('[[PROID]]', $row[products_id], $getEvents);
						$det_hold = $this->db_fetch_array($this->db_query($query, $link));
						$det_hold[qnty] = $row[qnty];
						$cart_details[] = $det_hold;
						$errors[] = array(mysql_error($link),  $query);
					}
			}
			return array('cart'=>$cart_details, 'errors'=>$errors);
			//return array('sessions'=>$sessions, 'basket'=>$basket, 'errors'=>$errors);
			//return $sql;	
		}
		

	}
	
	


?>